<?php
namespace Com\Pay;

//得成微信扫码支付

class DechengSMPay{

    private $config = array();

    public function __construct($mchData){
        $this->config = $mchData;
    }



    /*生成支付连接*/
    public function getUrl($order_info){
        $merchantCode = $this->config['mch_id'];
        $notifyUrl = S_URL . '/Api/NotifyCallback/DechengNotify';
        $order_sn = $order_info['ordernumber'];
        $price = $order_info['paymoney'] * 100;
        $post_data = array(
            'type' =>   'SaoMa',
            'version'    =>    '1.0.0',
            'signType'     =>  'MD5',
            "userId"        =>  $merchantCode,
            "requestId"  =>  $order_sn,
            "orderAmount"  =>  $price,
            "orderTitle"  =>  "DechengSaoMa",
            "tranCode"     => 'WXSM',
            'callBackUrl'   => $notifyUrl,
            'accountType'   => '0'
        );
        $post_data['signData'] = $this->setSign($post_data);
        $Send_url = 'http://pos.gzdcdata.com/dcpay';

        $res = json_decode($this->curl_json_post($Send_url,json_encode($post_data)),true);
        if($res['qrCodeUrl'] != ''){
            return $res;
        }else{
            // var_dump($res1);die;
            $ret = '';
        }
        return $ret;
    }

    private function setSign($data = array(),$isNotify){
        ksort($data);
        // 拼接签名字符串
        $str = "";
        foreach($data as $key1 => $val){
            if($isNotify && $key1 == 'hmac') continue; // sign不加入验签
            if($val != '') $str = $str . $key1 . '=' . $val . '&';
        }
        $signTmpStr = trim($str,'&'); // 待加密的字符串
        $sign = $isNotify ? md5($signTmpStr . '&key=' .$this->config['mch_key']) : strtoupper(md5($signTmpStr . '&key=' .$this->config['mch_key']));
        return $sign;
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = parseData();
        $hamc = $data['hmac'];
        // 进行签名验证
        $Sign =  $this->setSign($data,true);
        if($hamc === $Sign){
            // 验签成功
            if($data['returnCode'] == '000000' && $data['ordersts'] == 'S'){
                return true;
            }else{
                return false;
            }
        }else{
            // 失败
            return false;
        }
    }


    public function curl_json_post($url,$data)
    {
        if(is_array($data)){
            $data_string = json_encode($data);
        }else{
            $data_string = $data;
        }
        // echo $data_string;die;
        $ch = curl_init($url);
        // curl_setopt($ch, CURLOPT_TIMEOUT,5);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
        );
        $result = curl_exec($ch);
        curl_close ( $ch );
        return $result;
    }


}
